using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The EmployeeJobExperiencesTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeJobExperiencesTable objects.
/// </summary>
public class EmployeeJobExperiencesTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeJobExperiencesTable from the underlying datasource.
/// </summary>
/// <param name="employeeJobExperienceKey">The unique EmployeeJobExperienceKey of the EmployeeJobExperiencesTable in the database.</param>
/// <returns>An EmployeeJobExperiencesTable when the EmployeeJobExperienceKey was found in the database, or null otherwise.</returns>
public static EmployeeJobExperiencesTable GetItem(int employeeJobExperienceKey)
{
EmployeeJobExperiencesTable myEmployeeJobExperiencesTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeJobExperiencesTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeJobExperienceKey", employeeJobExperienceKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeJobExperiencesTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeJobExperiencesTable;
}
}

/// <summary>
/// Returns a list with EmployeeJobExperiencesTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeJobExperiencesTable objects.</returns>
public static EmployeeJobExperiencesTableList GetList()
{
EmployeeJobExperiencesTableList tempList = new EmployeeJobExperiencesTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeJobExperiencesTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeJobExperiencesTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeJobExperiencesTable objects.</returns>
public static EmployeeJobExperiencesTableList GetListByUserAccountKey(int UserAccountKey)
{
EmployeeJobExperiencesTableList tempList = new EmployeeJobExperiencesTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeJobExperiencesTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a EmployeeJobExperiencesTable in the database.
/// </summary>
/// <param name="myEmployeeJobExperiencesTable">The EmployeeJobExperiencesTable instance to save.</param>
/// <returns>The new EmployeeJobExperienceKey if the EmployeeJobExperiencesTable is new in the database or the existing EmployeeJobExperienceKey when an item was updated.</returns>
public static int Save(EmployeeJobExperiencesTable myEmployeeJobExperiencesTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeJobExperiencesTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeJobExperiencesTable.EmployeeJobExperienceKey == -1)
{myCommand.Parameters.AddWithValue("@employeeJobExperienceKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeJobExperienceKey", myEmployeeJobExperiencesTable.EmployeeJobExperienceKey);
}
if (myEmployeeJobExperiencesTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeJobExperiencesTable.UserAccountKey);
}
if (myEmployeeJobExperiencesTable.PreviousEmployer == null){
myCommand.Parameters.AddWithValue("@PreviousEmployer", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@PreviousEmployer", myEmployeeJobExperiencesTable.PreviousEmployer);
}
if (myEmployeeJobExperiencesTable.StartDate == null){
myCommand.Parameters.AddWithValue("@startDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@startDate", myEmployeeJobExperiencesTable.StartDate);
}
if (myEmployeeJobExperiencesTable.EndDate == null){
myCommand.Parameters.AddWithValue("@endDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@endDate", myEmployeeJobExperiencesTable.EndDate);
}
if (myEmployeeJobExperiencesTable.Position == null){
    myCommand.Parameters.AddWithValue("@position", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@position", myEmployeeJobExperiencesTable.Position);
}
if (myEmployeeJobExperiencesTable.BasicSalary == null){
myCommand.Parameters.AddWithValue("@basicSalary", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@basicSalary", myEmployeeJobExperiencesTable.BasicSalary);
}
if (myEmployeeJobExperiencesTable.RefereeName == null){
myCommand.Parameters.AddWithValue("@refereeName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@refereeName", myEmployeeJobExperiencesTable.RefereeName);
}
if (myEmployeeJobExperiencesTable.RefereeAddress == null){
myCommand.Parameters.AddWithValue("@refereeAddress", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@refereeAddress", myEmployeeJobExperiencesTable.RefereeAddress);
}
if (myEmployeeJobExperiencesTable.RefereeContact == null){
myCommand.Parameters.AddWithValue("@refereeContact", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@refereeContact", myEmployeeJobExperiencesTable.RefereeContact);
}
if (myEmployeeJobExperiencesTable.RefereeEmail == null){
myCommand.Parameters.AddWithValue("@refereeEmail", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@refereeEmail", myEmployeeJobExperiencesTable.RefereeEmail);
}
if (myEmployeeJobExperiencesTable.Remarks == null){
myCommand.Parameters.AddWithValue("@remarks", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remarks", myEmployeeJobExperiencesTable.Remarks);
}
if (myEmployeeJobExperiencesTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeJobExperiencesTable.Status);
}
if (myEmployeeJobExperiencesTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeJobExperiencesTable.CreatedDateTime);
}
if (myEmployeeJobExperiencesTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeJobExperiencesTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeJobExperiencesTable from the database.
/// </summary>
/// <param name="employeeJobExperienceKey">The EmployeeJobExperienceKey of the EmployeeJobExperiencesTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int employeeJobExperienceKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeJobExperiencesTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeJobExperienceKey", employeeJobExperienceKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeJobExperiencesTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeJobExperiencesTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeJobExperiencesTable myEmployeeJobExperiencesTable = new EmployeeJobExperiencesTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeJobExperienceKey")))
{
myEmployeeJobExperiencesTable.EmployeeJobExperienceKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeJobExperienceKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeJobExperiencesTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PreviousEmployer")))
{
myEmployeeJobExperiencesTable.PreviousEmployer = myDataRecord.GetString(myDataRecord.GetOrdinal("PreviousEmployer"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
{
myEmployeeJobExperiencesTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
{
myEmployeeJobExperiencesTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Position")))
{
    myEmployeeJobExperiencesTable.Position = myDataRecord.GetString(myDataRecord.GetOrdinal("Position"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BasicSalary")))
{
myEmployeeJobExperiencesTable.BasicSalary = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("BasicSalary"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RefereeName")))
{
myEmployeeJobExperiencesTable.RefereeName = myDataRecord.GetString(myDataRecord.GetOrdinal("RefereeName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RefereeAddress")))
{
myEmployeeJobExperiencesTable.RefereeAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("RefereeAddress"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RefereeContact")))
{
myEmployeeJobExperiencesTable.RefereeContact = myDataRecord.GetString(myDataRecord.GetOrdinal("RefereeContact"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RefereeEmail")))
{
myEmployeeJobExperiencesTable.RefereeEmail = myDataRecord.GetString(myDataRecord.GetOrdinal("RefereeEmail"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remarks")))
{
myEmployeeJobExperiencesTable.Remarks = myDataRecord.GetString(myDataRecord.GetOrdinal("Remarks"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeJobExperiencesTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myEmployeeJobExperiencesTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myEmployeeJobExperiencesTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEmployeeJobExperiencesTable;
}

 
}
